package com.talkingdata.hadoop.guard.mapper;

import com.talkingdata.hadoop.guard.bean.AppFinishOffset;
import com.talkingdata.hadoop.guard.bean.ApplicationState;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ApplicationMapper {

    @Select("select max(finished_time) from application_state where cluster_id = #{clusterid}")
    long selectMaxFinishedTimeByClusterId(String clusterid);

    @Insert("insert into application_state (cluster_id,application_id,user,name," +
            "application_type,queue,started_time,finished_time,state," +
            "final_status,memory_seconds,vcore_seconds,queue_usage_percentage," +
            "tracking_url,timestamp) values (#{clusterId},#{applicationId}," +
            "#{user},#{name},#{applicationType},#{queue},#{startedTime},#{finishedTime}," +
            "#{state},#{finalStatus},#{memorySeconds},#{vcoreSeconds},#{queueUsagePercentage}," +
            "#{trackingUrl},#{timestamp})")
    int insertApplication(ApplicationState applicationState);

    @Select("select cluster_id,application_id,finished_time,timestamp from application_state where finished_time = #{finished_time} and  cluster_id = #{clusterid}")
    AppFinishOffset selectMaxAppByFinishedTime(@Param("clusterid") String clusterId, @Param("finished_time") Long finishedtime);
}
